//
//  ReportChartCell.m
//  efairy
//
//  Created by 李锋 on 2019/3/19.
//  Copyright © 2019年 East. All rights reserved.
//

#import "ReportChartCell.h"
#import <Highcharts/Highcharts.h>


@implementation ReportChartCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self initView];
    }
    return self;
}

- (void)setFrame:(CGRect)frame{
    frame.origin.x += 8.0f;
    frame.size.width -=2*8.0f;
    [super setFrame:frame];
}

-(void)initView{
    _titleLable = [[UILabel alloc]initWithFrame:CGRectMake(12, self.contentView.frame.origin.y, SCREEN_WIDTH-16-12, 40)];
    _titleLable.font = [UIFont boldSystemFontOfSize:15.0f];
    _titleLable.textColor = ColorFromRGB(0x333333);
    _titleLable.backgroundColor = [UIColor whiteColor];
    _line = [[UIView alloc]initWithFrame:CGRectMake(0, _titleLable.frame.origin.y+40, SCREEN_WIDTH-16, 1)];
    _line.backgroundColor = ColorFromRGB(0xe5e5e5);
    _nodataLaebl = [[UILabel alloc]initWithFrame:CGRectMake(0, _line.frame.origin.y+1, SCREEN_WIDTH-16, 40)];
    _nodataLaebl.font = [UIFont systemFontOfSize:15.0f];
    _nodataLaebl.textColor = ColorFromRGB(0x666666);
    _nodataLaebl.textAlignment = NSTextAlignmentCenter;
    _nodataLaebl.backgroundColor = [UIColor whiteColor];
    _nodataLaebl.text = @"暂无图表统计数据";
    _nodataLaebl.hidden = YES;
    self.contentView.backgroundColor = [UIColor whiteColor];
   [self.contentView sd_addSubviews:@[_titleLable,_line,_nodataLaebl]];
}

- (void)configViewWithRptAlarmScaleItem:(AlarmStatisticsList *)item {
    _titleLable.text =  item.efairydevice_device_type;
    if(item.pieData.count > 0){
        _chartView = [[HIChartView alloc]initWithFrame:CGRectMake(0,_line.frame.origin.y+1, SCREEN_WIDTH-16, 173)];
        [self.contentView addSubview:_chartView];
        [self setupAutoHeightWithBottomView:_chartView bottomMargin:0];
        HIChart *chart = [[HIChart alloc] init];
        chart.type = @"pie";
        
        HITitle *title = [[HITitle alloc] init];
        title.text= @"报警占比";
        title.align = @"left";
        title.style = ({
            HIStyle *style = [[HIStyle alloc] init];
            style.color = @"#333333";
            style.fontSize = @"15px";
//            style.fontWeight =@"bold";
            style;
        });
        
        HITooltip *tooltip = [[HITooltip alloc]init];
        tooltip.pointFormat = @"{series.name}: <b>{point.percentage:.1f}%</b>";
        
        HIPlotOptions *plotOptions = [[HIPlotOptions alloc]init];
        plotOptions.pie = [[HIPie alloc]init];
        plotOptions.pie.allowPointSelect = [[NSNumber alloc] initWithBool:true];
        plotOptions.pie.cursor = @"pointer";
        plotOptions.pie.dataLabels = [[HIDataLabels alloc]init];
        plotOptions.pie.dataLabels.enabled = [[NSNumber alloc] initWithBool:true];
        plotOptions.pie.dataLabels.format = @"<b>{point.name}</b>: {point.percentage:.1f} %";
        plotOptions.pie.dataLabels.style.color = @"black";
        
        HIPie *pie = [[HIPie alloc] init];
        pie.showInLegend = @(YES);
        pie.data = item.pieData;
        // HIExporting
        HIExporting *exporting = [[HIExporting alloc] init];
        exporting.enabled = @(NO);
       
        // 隐藏Logo
        HICredits *credits = [[HICredits alloc] init];
        credits.enabled = @(NO);
        
        HIOptions *options = [[HIOptions alloc]init];
        options.chart = chart;
        options.title = title;
        options.tooltip = tooltip;
        options.plotOptions = plotOptions;
        options.series =  @[pie];
        options.exporting = exporting;
        options.credits = credits;
        self.chartView.options = options;
        [self showColumnChart:item];
    }else{
        _nodataLaebl.hidden = NO;
        [self setupAutoHeightWithBottomView:_nodataLaebl bottomMargin:0];
        
    }
}

-(void)showColumnChart:(AlarmStatisticsList *)item{
    _line2 = [[UIView alloc]initWithFrame:CGRectMake(0, _chartView.frame.origin.y+173, SCREEN_WIDTH-16, 1)];
    _line2.backgroundColor = ColorFromRGB(0xe5e5e5);
     [self.contentView addSubview:_line2];
     _columnChartView = [[HIChartView alloc]initWithFrame:CGRectMake(0,_line2.frame.origin.y+1, SCREEN_WIDTH-16, 214)];
    [self.contentView addSubview:_columnChartView];
    [self setupAutoHeightWithBottomView:_columnChartView bottomMargin:0];
    HIOptions *options = [[HIOptions alloc]init];
    
    HIChart *chart = [[HIChart alloc]init];
    chart.type = @"column";
    
    HITitle *title = [[HITitle alloc] init];
    title.text= @"报警次数统计";
    title.align = @"left";
    title.style = ({
        HIStyle *style = [[HIStyle alloc] init];
        style.color = @"#333333";
        style.fontSize = @"15px";
//        style.fontWeight =@"bold";
        style;
    });
    
    
    HIXAxis *xaxis = [[HIXAxis alloc]init];
    xaxis.type = @"category";
    xaxis.labels = [[HILabels alloc]init];
    xaxis.labels.rotation = @-45;
//    xaxis.labels.style = [[HICSSObject alloc] init];
//    xaxis.labels.style.fontSize = @"13px";
//    xaxis.labels.style.fontFamily = @"Verdana, sans-serif";
    
    HIYAxis *yaxis = [[HIYAxis alloc]init];
    yaxis.min = @0;
//    yaxis.title = [[HITitle alloc]init];
//    yaxis.title.text = @"Population (millions)";
//
    HILegend *legend = [[HILegend alloc]init];
    legend.enabled = [[NSNumber alloc] initWithBool:false];
    
//    HITooltip *tooltip = [[HITooltip alloc]init];
//    tooltip.pointFormat = @"Population in 2008: <b>{point.y:.1f} millions</b>";
    
    HIColumn *column = [[HIColumn alloc]init];
    column.name = @"报警次数";
    column.data = item.columnData;
    column.dataLabels = [[HIDataLabels alloc]init];
    column.dataLabels.enabled = [[NSNumber alloc] initWithBool:true];
//    column.dataLabels.rotation = @-90;
    column.dataLabels.color = [[HIColor alloc]initWithHexValue:@"FFFFFF"];
    column.dataLabels.align = @"center";
//    column.dataLabels.format = @"{point.y:.1f}";
//    column.dataLabels.y = @10;
    column.borderWidth = @18;
//    column.dataLabels.style = [[HICSSObject alloc] init];
    column.dataLabels.style.fontSize = @"13px";
    column.dataLabels.style.fontFamily = @"Verdana, sans-serif";
    
    
    // HIExporting
    HIExporting *exporting = [[HIExporting alloc] init];
    exporting.enabled = @(NO);
    
    HICredits *credits = [[HICredits alloc] init];
    credits.enabled = @(NO);
    
    options.chart = chart;
    options.title = title;
//    options.subtitle = subtitle;
    options.xAxis = [NSMutableArray arrayWithObjects:xaxis, nil];
    options.yAxis = [NSMutableArray arrayWithObjects:yaxis, nil];
    options.legend = legend;
    options.credits = credits;
    options.exporting = exporting;
//    options.tooltip = tooltip;
    options.series = [NSMutableArray arrayWithObjects: column, nil];
    _columnChartView.options = options;
    
}





@end
